package ru.scan2d.main.vizualizer.impl;

import java.awt.Container;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JFileChooser;

import ru.scan2d.main.MainWindowPresenter;
import ru.scan2d.main.MainWindowPresenter.Listener;
import ru.scan2d.main.core.FileProcessor;
import ru.scan2d.main.system.ClientFactory;
import ru.scan2d.main.util.ReportUtil;
import ru.scan2d.main.util.SettingsUtil;
import ru.scan2d.main.util.TransferableImage;
import ru.scan2d.main.vizualizer.VisualizerPresenter;

public class VisualizerPresenterImpl implements VisualizerPresenter {

	private VisualizerViewImpl view;
	private PicturePanelImpl picturePanel;
	private Listener listener;
	JFileChooser fileOpenChooser;
	JFileChooser fileSaveChooser;

	public VisualizerPresenterImpl(ClientFactory clientFactory,
			MainWindowPresenter.Listener listener) {
		view = new VisualizerViewImpl();
		view.setListener(listener);
		this.listener = listener;
		fileOpenChooser = clientFactory.getOpenFileChooser();
		fileSaveChooser = clientFactory.getSaveFileChooser();
	}

	@Override
	public void go(Container container) {
		initView();
	}

	private void initView() {
		listener.addCenterPanel(view);
		initFile();
	}

	private void initFile() {
		open();
	}

	@Override
	public void copyImagePart(BufferedImage img, ClipboardOwner owner) {
		TransferableImage trans = new TransferableImage(img);
		Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
		c.setContents(trans, owner);
	}

	@Override
	public void saveImagePart(BufferedImage img) {
		int ret = fileSaveChooser.showDialog(null, "Save");
		if (ret == JFileChooser.APPROVE_OPTION) {
			File file = fileSaveChooser.getSelectedFile();
			ReportUtil.saveImage(img, file.getAbsolutePath());
			fileSaveChooser.setCurrentDirectory(file.getAbsoluteFile());
			SettingsUtil.setProperty(SettingsUtil.SAVE_CHOOSER, file
					.getAbsoluteFile().getAbsolutePath());
		}
	}

	public void open() {
		int ret = fileOpenChooser.showDialog(null, "Open");
		if (ret == JFileChooser.APPROVE_OPTION) {
			File file = fileOpenChooser.getSelectedFile();
			FileProcessor fp = new FileProcessor(file);
			PicturePanelImpl picturePanel = new PicturePanelImpl(fp);
			picturePanel.setListener(listener);
			picturePanel.setPresenter(this);
			picturePanel.setImage(fp.getImage(1));
			view.addPicture(picturePanel, file.getAbsolutePath());
			listener.needRepaint();
			fileOpenChooser.setCurrentDirectory(file.getAbsoluteFile());
			SettingsUtil.setProperty(SettingsUtil.OPEN_CHOOSER, file
					.getAbsoluteFile().getAbsolutePath());
		}
	}

	public void save() {
		int ret = fileSaveChooser.showDialog(null, "Save");
		if (ret == JFileChooser.APPROVE_OPTION) {
			File file = fileSaveChooser.getSelectedFile();
			ReportUtil.saveImage(view.getCurrentPicture(),
					file.getAbsolutePath());
			fileSaveChooser.setCurrentDirectory(file.getAbsoluteFile());
			SettingsUtil.setProperty(SettingsUtil.SAVE_CHOOSER, file
					.getAbsoluteFile().getAbsolutePath());
		}
	}

	public void clear() {
		view.clearSelectedPanels();
	}
}
